﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace 用户模式自旋锁之SpinLock
{
    class Program
    {
        public static SpinLock spin = new SpinLock();
        public static int num = 0;

        static void Main(string[] args)
        {
            for (int i = 0; i < 5; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    Run();
                });
            }
            Console.Read();
        }



        public static void Run()
        {
            for (int i = 0; i < 100; i++)
            {
                try
                {
                    var b = false;
                    spin.Enter(ref b);
                    Console.WriteLine(num++);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                { 
                    spin.Exit();    
                }
            }
        }

    }
}
